package com.pur.zsms.check.web;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.check.service.ICheckService;
import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.unusual.vo.DealUnususlVo;
import com.pur.zsms.unusual.vo.OrderSupplierMaterUnusualVo;

/**
 * 查看模块
 * 
 * @author zhou
 * 
 */
@Controller
@RequestMapping(value = "/check")
public class CheckController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(CheckController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "checkService")
	private ICheckService checkService;

	@Resource(name = "userService")
	private IUserService userService;

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@RequestMapping(value = "/findCheck.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findCheck(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CheckVo checkVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<CheckVo> checkList = new ArrayList<CheckVo>();

		if (checkVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			json.put("rows", checkList);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = checkService.listCheck(pageController, checkVo);
		// 更具检索条件查询总价钱 为了计算百分比
		List<Object> listSum = checkService.getSumValueForCheck(checkVo);
		BigDecimal sumValue = new BigDecimal(0);
		boolean percentflag = false;
		if (listSum != null && listSum.size() > 0 && listSum.get(0) != null) {
			if (((BigDecimal) listSum.get(0)).compareTo(new BigDecimal(0)) != 0) {
				sumValue = (BigDecimal) listSum.get(0);
				percentflag = true;
			}
		}

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CheckVo f = new CheckVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setUnit((String) item[2]);
				f.setSpecifications((String) item[3]);
				f.setBrands((String) item[4]);
				f.setMaterWeightAll((BigDecimal) item[5]);
				f.setMaterValueAll((BigDecimal) item[6]);
				if (item[5] == null || item[6] == null) {
					f.setMaterValueAllEvey(new BigDecimal(0));
				} else {
					f.setMaterValueAllEvey(((BigDecimal) item[6]).divide((BigDecimal) item[5], 2, BigDecimal.ROUND_HALF_UP));
				}
				f.setMaterTypeString(item[7].toString());
				// 设置百分比
				if (percentflag && (BigDecimal) item[6] != null) {
					String percent = ((((BigDecimal) item[6]).multiply(new BigDecimal(100))).divide(sumValue, 2, BigDecimal.ROUND_HALF_UP).toString())
							+ "%";
					f.setPercent(percent);
				}

				// 设置检索信息
				f.setOrdDateStart(checkVo.getOrdDateStart());
				f.setOrdDateEnd(checkVo.getOrdDateEnd());
				f.setCustIdAll(checkVo.getCustIdAll());
				f.setSupId(checkVo.getSupId());
				f.setContent("查看详细内容");

				checkList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", checkList);
		return json.toString();
	}

	@RequestMapping(value = "/findCheckForSupplier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findCheckForSupplier(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CheckVo checkVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<CheckVo> checkList = new ArrayList<CheckVo>();

		if (checkVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			json.put("rows", checkList);
			return json.toString();
		}
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = checkService.listCheckForSupplier(pageController, checkVo);
		// 更具检索条件查询总价钱 为了计算百分比
		List<Object> listSum = checkService.getSumValueForCheck(checkVo);
		BigDecimal sumValue = new BigDecimal(0);
		boolean percentflag = false;
		if (listSum != null && listSum.size() > 0 && listSum.get(0) != null) {
			if (((BigDecimal) listSum.get(0)).compareTo(new BigDecimal(0)) != 0) {
				sumValue = (BigDecimal) listSum.get(0);
				percentflag = true;
			}
		}

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CheckVo f = new CheckVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterName((String) item[1]);
				f.setUnit((String) item[2]);
				f.setSpecifications((String) item[3]);
				f.setBrands((String) item[4]);
				f.setMaterWeightAll((BigDecimal) item[5]);
				f.setMaterValueAll((BigDecimal) item[6]);
				if (item[7] != null && !item[7].equals("") && Double.valueOf(item[7].toString()) != 0) {
					f.setMaterValueAllCost(new BigDecimal(item[7].toString()));
				} else {
					f.setMaterValueAllCost(new BigDecimal(0));
				}

				if (item[5] == null || item[6] == null || Double.valueOf(item[5].toString()) == 0 || Double.valueOf(item[6].toString()) == 0) {
					f.setMaterValueAllEvey(new BigDecimal(0));
				} else {
					f.setMaterValueAllEvey(((BigDecimal) item[6]).divide((BigDecimal) item[5], 2, BigDecimal.ROUND_HALF_UP));
				}

				if (item[5] == null || item[7] == null || Double.valueOf(item[5].toString()) == 0 || Double.valueOf(item[7].toString()) == 0) {
					f.setMaterValueAllEveyCost(new BigDecimal(0));
				} else {
					f.setMaterValueAllEveyCost((new BigDecimal(item[7].toString())).divide((BigDecimal) item[5], 2, BigDecimal.ROUND_HALF_UP));
				}

				// 设置百分比
				if (percentflag && (BigDecimal) item[6] != null) {
					String percent = ((((BigDecimal) item[6]).multiply(new BigDecimal(100))).divide(sumValue, 2, BigDecimal.ROUND_HALF_UP).toString())
							+ "%";
					f.setPercent(percent);
				}

				f.setMaterTypeString((String) item[8]);
				// 设置检索信息
				f.setOrdDateStart(checkVo.getOrdDateStart());
				f.setOrdDateEnd(checkVo.getOrdDateEnd());
				f.setCustIdAll(checkVo.getCustIdAll());
				f.setSupId(checkVo.getSupId());
				f.setContent("查看详细内容");

				checkList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", checkList);
		return json.toString();
	}

	/**
	 * 客户审核页面的详细内容
	 * */
	@RequestMapping(value = "/goCheckDetailedForAdmin.do", method = { RequestMethod.GET })
	public ModelAndView goCheckDetailedForAdmin(CheckVo checkVo, HttpSession session) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();

		try {

			if (checkVo.getCustIdAll() != null && !"0".equals(checkVo.getCustIdAll()) && !"".equals(checkVo.getCustIdAll())) {

				List<ShopPo> list = userService.getUsePoListByUserIdString(checkVo.getCustIdAll());
				if (list != null && list.size() > 0) {
					StringBuffer custIdAllbuffer = new StringBuffer("");
					for (ShopPo shopPo : list) {
						custIdAllbuffer.append(shopPo.getShopName());
						custIdAllbuffer.append(",");
					}
					String custIdAll = custIdAllbuffer.substring(0, custIdAllbuffer.length() - 1);
					checkVo.setCustName(custIdAll);
				}
			} else {
				checkVo.setCustIdAll(null);
				checkVo.setCustName("全部");
			}

			if ("sup".equals(checkVo.getUser())) {
				UserPo lgUser = (UserPo) session.getAttribute("user");
				int supId = supplierService.getSupIdByUserId(lgUser.getUserId());
				checkVo.setSupId(supId);
			}

			if (checkVo.getSupId() == 0) {
				checkVo.setSupName("全部");
			} else {
				checkVo.setSupName(supplierService.getSupPoBySupId(checkVo.getSupId()).getCompanyName());
			}
			checkVo.setMaterName(materService.getMaterPo(checkVo.getMaterId()).getMaterName());
			map.put("checkVo", checkVo);

			if ("sup".equals(checkVo.getUser())) {
				return new ModelAndView("/check/checkEveryOrderDetailForSup", map);
			} else {
				return new ModelAndView("/check/checkEveryOrderDetail", map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
			return new ModelAndView("/base/error", map);
		}
	}

	@RequestMapping(value = "/findCheckDetail.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findCheckDetail(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CheckVo checkVo)
			throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<CheckVo> checkList = new ArrayList<CheckVo>();
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = checkService.findCheckDetail(pageController, checkVo);
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CheckVo f = new CheckVo();
				f.setOrdId((Integer) item[0]);
				f.setOrdCustId((Integer) item[1]);
				f.setOrdCustName((String) item[2]);
				f.setOrdDate((String) item[3]);
				f.setMaterId((Integer) item[4]);
				f.setMaterName((String) item[5]);
				f.setSpecifications((String) item[6]);
				f.setBrands((String) item[7]);
				f.setOrdMaterWeight((BigDecimal) item[8]);
				f.setUnit((String) item[9]);
				f.setOrdMaterValue((BigDecimal) item[10]);
				if (item[10] == null || item[8] == null || Double.valueOf(item[8].toString()) == 0) {
					f.setOrdMaterValueEvery(new BigDecimal(0));
				} else {
					f.setOrdMaterValueEvery(((BigDecimal) item[10]).divide((BigDecimal) item[8], 2, BigDecimal.ROUND_HALF_UP));
				}

				f.setSupId((Integer) item[11]);
				f.setSupName((String) item[12]);
				f.setCode((String) item[13]);

				checkList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", checkList);
		return json.toString();
	}

	// 导出批量查看报表excel
	@RequestMapping(value = "/exportCheckExcel.do", method = { RequestMethod.POST })
	public void exportMaterExcel(HttpSession session, HttpServletResponse response, CheckVo checkVo) {

		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excel", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = session.getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator") + "excelCheck.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			// 起始日期
			HSSFRow row1 = sheet.getRow(1);
			HSSFCell cell1FromRow1 = row1.getCell((int) 1);//
			cell1FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow1.setCellValue(checkVo.getOrdDateStart());
			cell1FromRow1.setCellStyle(setBorder);
			// 截止日期
			HSSFCell cell3FromRow1 = row1.getCell((int) 3);//
			cell3FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3FromRow1.setCellValue(checkVo.getOrdDateEnd());

			// 种类
			HSSFCell cell4FromRow1 = row1.getCell((int) 5);//
			cell4FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getMaterType() != 0) {
				dataDictionaryService.getMaterTypeName(checkVo.getMaterType());
				cell4FromRow1.setCellValue(dataDictionaryService.getMaterTypeName(checkVo.getMaterType()));
			} else {
				cell4FromRow1.setCellValue("全部");
			}
			// 材料名称
			HSSFCell cell5FromRow1 = row1.getCell((int) 7);//
			cell5FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getMaterName() != null && !checkVo.getMaterName().equals("")) {
				cell5FromRow1.setCellValue(checkVo.getMaterName());
			} else {
				cell5FromRow1.setCellValue("全部");
			}

			// 客户
			HSSFCellStyle setBorderForcust = workbook.createCellStyle();
			setBorderForcust.setWrapText(true); // 自动换行
			setBorderForcust.setVerticalAlignment(setBorder.VERTICAL_CENTER);// 垂直居中
			setBorderForcust.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderForcust.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderForcust.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderForcust.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderForcust.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

			HSSFRow row2 = sheet.getRow(2);
			HSSFCell cell1FromRow2 = row2.getCell((int) 1);//
			cell1FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow2.setCellValue(checkVo.getCustName());
			cell1FromRow2.setCellStyle(setBorderForcust);

			// 供货商
			HSSFRow row3 = sheet.getRow(3);
			HSSFCell cell1FromRow3 = row3.getCell((int) 1);//
			cell1FromRow3.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getSupId() == 0) {
				cell1FromRow3.setCellValue("全部");
			} else {
				cell1FromRow3.setCellValue(supplierService.getSupPoBySupId(checkVo.getSupId()).getCompanyName());
			}
			cell1FromRow3.setCellStyle(setBorderForcust);

			List<Object[]> listTotal = checkService.getListCheckForExcel(checkVo);
			List<Object> listSum = checkService.getSumValueForCheck(checkVo);
			BigDecimal sumValue = new BigDecimal(0);
			boolean percentflag = false;
			if (listSum != null && listSum.size() > 0) {
				sumValue = (BigDecimal) listSum.get(0);
				percentflag = true;
			}

			for (int i = 0; i < listTotal.size(); i++) {

				Object[] a = listTotal.get(i);

				HSSFRow row = sheet.createRow((int) i + 5);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[0].toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell11 = row.createCell((int) 1);// 创建一列
				cell11.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell11.setCellValue(a[8].toString());
				cell11.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 2);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(a[1].toString());
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 3);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(a[3].toString());
				cell3.setCellStyle(setBorder);

				HSSFCell cell4 = row.createCell((int) 4);// 创建一列
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellValue(a[4].toString());
				cell4.setCellStyle(setBorder);

				HSSFCell cell5 = row.createCell((int) 5);// 创建一列
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellValue(a[5].toString());
				cell5.setCellStyle(setBorder);

				HSSFCell cell6 = row.createCell((int) 6);// 创建一列
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellValue(a[2].toString());
				cell6.setCellStyle(setBorder);

				HSSFCell cell9 = row.createCell((int) 7);// 创建一列
				cell9.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null && !a[6].equals("0.00") && !a[6].equals("0") && a[5] != null && !a[5].equals("0.00") && !a[5].equals("0")) {

					cell9.setCellValue((new BigDecimal(a[6].toString())).divide(new BigDecimal(a[5].toString()), 2, BigDecimal.ROUND_HALF_UP)
							.toString());
				} else {
					cell9.setCellValue("0.00");
				}
				cell9.setCellStyle(setBorder);

				HSSFCell cell7 = row.createCell((int) 8);// 创建一列
				cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null) {
					cell7.setCellValue(a[6].toString());
				} else {
					cell7.setCellValue("0.00");
				}
				cell7.setCellStyle(setBorder);

				HSSFCell cell8 = row.createCell((int) 9);// 创建一列
				cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null && percentflag && sumValue.compareTo(new BigDecimal(0)) != 0) {
					String percent = (((new BigDecimal(a[6].toString())).multiply(new BigDecimal(100))).divide(sumValue, 2, BigDecimal.ROUND_HALF_UP)
							.toString()) + "%";
					cell8.setCellValue(percent);
				} else {
					cell8.setCellValue("0.00%");
				}
				cell8.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	// 导出报表excel
	@RequestMapping(value = "/exportCheckExcelDetail.do", method = { RequestMethod.POST })
	public void exportCheckExcelDetail(HttpSession session, HttpServletResponse response, CheckVo checkVo) {

		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excel", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = session.getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator") + "excelCheckDetail.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			// 起始日期
			HSSFRow row1 = sheet.getRow(1);
			HSSFCell cell1FromRow1 = row1.getCell((int) 1);//
			cell1FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow1.setCellValue(checkVo.getOrdDateStart());
			cell1FromRow1.setCellStyle(setBorder);
			// 截止日期
			HSSFCell cell3FromRow1 = row1.getCell((int) 3);//
			cell3FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3FromRow1.setCellValue(checkVo.getOrdDateEnd());

			// 材料id
			HSSFCell cell4FromRow1 = row1.getCell((int) 5);//
			cell4FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell4FromRow1.setCellValue(checkVo.getMaterId());
			// 材料名称
			HSSFCell cell5FromRow1 = row1.getCell((int) 7);//
			cell5FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell5FromRow1.setCellValue(checkVo.getMaterName());
			// 客户
			HSSFCellStyle setBorderForcust = workbook.createCellStyle();
			setBorderForcust.setWrapText(true); // 自动换行
			setBorderForcust.setVerticalAlignment(setBorder.VERTICAL_CENTER);// 垂直居中
			setBorderForcust.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderForcust.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderForcust.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderForcust.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderForcust.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

			HSSFRow row2 = sheet.getRow(2);
			HSSFCell cell1FromRow2 = row2.getCell((int) 1);//
			cell1FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow2.setCellValue(checkVo.getCustName());
			cell1FromRow2.setCellStyle(setBorderForcust);

			// 供货商
			// 供货商
			HSSFRow row3 = sheet.getRow(3);
			HSSFCell cell1FromRow3 = row3.getCell((int) 1);//
			cell1FromRow3.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getSupId() == 0) {
				cell1FromRow3.setCellValue("全部");
			} else {
				cell1FromRow3.setCellValue(supplierService.getSupPoBySupId(checkVo.getSupId()).getCompanyName());
			}
			cell1FromRow3.setCellStyle(setBorderForcust);

			List<Object[]> listTotal = checkService.getListCheckForExcelDetail(checkVo);
			for (int i = 0; i < listTotal.size(); i++) {

				Object[] a = listTotal.get(i);
				HSSFRow row = sheet.createRow((int) i + 5);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[0].toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(a[1].toString());
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 2);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(a[2].toString());
				cell3.setCellStyle(setBorder);

				HSSFCell cell4 = row.createCell((int) 3);// 创建一列
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellValue(a[11].toString());
				cell4.setCellStyle(setBorder);

				HSSFCell cell5 = row.createCell((int) 4);// 创建一列
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellValue(a[12].toString());
				cell5.setCellStyle(setBorder);

				HSSFCell cell6 = row.createCell((int) 5);// 创建一列
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellValue(a[3].toString());
				cell6.setCellStyle(setBorder);

				HSSFCell cell9 = row.createCell((int) 6);// 创建一列
				cell9.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell9.setCellValue(a[6].toString());
				cell9.setCellStyle(setBorder);

				HSSFCell cell10 = row.createCell((int) 7);// 创建一列
				cell10.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell10.setCellValue(a[7].toString());
				cell10.setCellStyle(setBorder);

				HSSFCell cell11 = row.createCell((int) 8);// 创建一列
				cell11.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell11.setCellValue(a[8].toString());
				cell11.setCellStyle(setBorder);

				HSSFCell cell12 = row.createCell((int) 9);// 创建一列
				cell12.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell12.setCellValue(a[9].toString());
				cell12.setCellStyle(setBorder);

				HSSFCell cell13 = row.createCell((int) 10);// 创建一列
				cell13.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[10] != null && a[8] != null && !a[8].equals("0") && !a[8].equals("0.00")) {
					cell13.setCellValue(((new BigDecimal(a[10].toString())).divide((BigDecimal) a[8], 2, BigDecimal.ROUND_HALF_UP)).toString());
				} else {
					cell13.setCellValue("0");
				}
				cell13.setCellStyle(setBorder);

				HSSFCell cell14 = row.createCell((int) 11);// 创建一列
				cell14.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[10] != null) {
					cell14.setCellValue((new BigDecimal(a[10].toString())).toString());
				} else {
					cell14.setCellValue("0");
				}
				cell14.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	// 供货商导出批量查看报表excel
	@RequestMapping(value = "/exportMaterExcelForSupplier.do", method = { RequestMethod.POST })
	public void exportMaterExcelForSupplier(HttpSession session, HttpServletResponse response, CheckVo checkVo) {

		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excel", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = session.getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator") + "excelCheckForSupplier.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			// 起始日期
			HSSFRow row1 = sheet.getRow(1);
			HSSFCell cell1FromRow1 = row1.getCell((int) 1);//
			cell1FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow1.setCellValue(checkVo.getOrdDateStart());
			cell1FromRow1.setCellStyle(setBorder);
			// 截止日期
			HSSFCell cell3FromRow1 = row1.getCell((int) 3);//
			cell3FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3FromRow1.setCellValue(checkVo.getOrdDateEnd());

			// 种类
			HSSFCell cell4FromRow1 = row1.getCell((int) 5);//
			cell4FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getMaterType() != 0) {
				dataDictionaryService.getMaterTypeName(checkVo.getMaterType());
				cell4FromRow1.setCellValue(dataDictionaryService.getMaterTypeName(checkVo.getMaterType()));
			} else {
				cell4FromRow1.setCellValue("全部");
			}
			// 材料名称
			HSSFCell cell5FromRow1 = row1.getCell((int) 7);//
			cell5FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getMaterName() != null && !checkVo.getMaterName().equals("")) {
				cell5FromRow1.setCellValue(checkVo.getMaterName());
			} else {
				cell5FromRow1.setCellValue("全部");
			}

			// 客户
			HSSFCellStyle setBorderForcust = workbook.createCellStyle();
			setBorderForcust.setWrapText(true); // 自动换行
			setBorderForcust.setVerticalAlignment(setBorder.VERTICAL_CENTER);// 垂直居中
			setBorderForcust.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderForcust.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderForcust.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderForcust.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderForcust.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

			HSSFRow row2 = sheet.getRow(2);
			HSSFCell cell1FromRow2 = row2.getCell((int) 1);//
			cell1FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow2.setCellValue(checkVo.getCustName());
			cell1FromRow2.setCellStyle(setBorderForcust);

			// 供货商
			HSSFRow row3 = sheet.getRow(3);
			HSSFCell cell1FromRow3 = row3.getCell((int) 1);//
			cell1FromRow3.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (checkVo.getSupId() == 0) {
				cell1FromRow3.setCellValue("全部");
			} else {
				cell1FromRow3.setCellValue(supplierService.getSupPoBySupId(checkVo.getSupId()).getCompanyName());
			}
			cell1FromRow3.setCellStyle(setBorderForcust);

			List<Object[]> listTotal = checkService.getListCheckForExcel(checkVo);
			List<Object> listSum = checkService.getSumValueForCheck(checkVo);
			BigDecimal sumValue = new BigDecimal(0);
			boolean percentflag = false;
			if (listSum != null && listSum.size() > 0) {
				sumValue = (BigDecimal) listSum.get(0);
				percentflag = true;
			}

			for (int i = 0; i < listTotal.size(); i++) {

				Object[] a = listTotal.get(i);

				System.out.println(a[0]);
				System.out.println(a[1]);

				HSSFRow row = sheet.createRow((int) i + 5);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[0].toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(a[1].toString());
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 2);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(a[3].toString());
				cell3.setCellStyle(setBorder);

				HSSFCell cell4 = row.createCell((int) 3);// 创建一列
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellValue(a[4].toString());
				cell4.setCellStyle(setBorder);

				HSSFCell cell5 = row.createCell((int) 4);// 创建一列
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellValue(Double.parseDouble(a[5].toString()));
				cell5.setCellStyle(setBorder);

				HSSFCell cell6 = row.createCell((int) 5);// 创建一列
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellValue(a[2].toString());
				cell6.setCellStyle(setBorder);
				// 进货价格设置
				HSSFCell cell10 = row.createCell((int) 6);// 创建一列
				cell10.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[7] != null && a[5] != null && !a[5].toString().equals("0.00") && !a[5].toString().equals("0")) {
					cell10.setCellValue(Double.parseDouble((new BigDecimal(a[7].toString())).divide(new BigDecimal(a[5].toString()), 2,
							BigDecimal.ROUND_HALF_UP).toString()));
				} else {
					cell10.setCellValue(Double.parseDouble("0.00"));
				}
				cell10.setCellStyle(setBorder);

				HSSFCell cell11 = row.createCell((int) 7);// 创建一列
				cell11.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[7] != null) {
					cell11.setCellValue(Double.parseDouble(a[7].toString()));
				} else {
					cell11.setCellValue(Double.parseDouble("0.00"));
				}
				cell11.setCellStyle(setBorder);
				// 销售价格设置
				HSSFCell cell9 = row.createCell((int) 8);// 创建一列
				cell9.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null && a[5] != null && !a[5].toString().equals("0.00") && !a[5].toString().equals("0")) {
					cell9.setCellValue(Double.parseDouble((new BigDecimal(a[6].toString())).divide(new BigDecimal(a[5].toString()), 2,
							BigDecimal.ROUND_HALF_UP).toString()));
				} else {
					cell9.setCellValue(Double.parseDouble("0.00"));
				}
				cell9.setCellStyle(setBorder);

				HSSFCell cell7 = row.createCell((int) 9);// 创建一列
				cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null) {
					cell7.setCellValue(Double.parseDouble(a[6].toString()));
				} else {
					cell7.setCellValue(Double.parseDouble("0.00"));
				}
				cell7.setCellStyle(setBorder);

				HSSFCell cell8 = row.createCell((int) 10);// 创建一列
				cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
				if (a[6] != null && percentflag && sumValue.compareTo(new BigDecimal(0)) != 0) {
					String percent = (((new BigDecimal(a[6].toString())).multiply(new BigDecimal(100))).divide(sumValue, 2, BigDecimal.ROUND_HALF_UP)
							.toString()) + "%";
					cell8.setCellValue(percent);
				} else {
					cell8.setCellValue("0.00%");
				}
				cell8.setCellStyle(setBorder);
				
				HSSFCell cell12 = row.createCell((int) 11);// 创建一列
				cell12.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell12.setCellValue(a[8].toString());
				cell12.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	// 查看供货商销售数量报表
	@RequestMapping(value = "/preSalesVolume.do", method = { RequestMethod.GET })
	public ModelAndView preSalesVolume(HttpSession session, CheckVo checkVo) {
		JSONObject json = new JSONObject();
		try {

			UserPo userInfo = (UserPo) session.getAttribute("user");
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			checkVo.setSupId(supId);
			// materValueVo = materService.getMaterValueVoCost(materValueVo);
			checkVo = checkService.getSalesVolume(checkVo);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("checkVo", checkVo);
		if (checkVo.getTimeType() == 2) {
			return new ModelAndView("/check/lookSalesVolume2", map);
		} else if (checkVo.getTimeType() == 4) {
			return new ModelAndView("/check/lookSalesVolume3", map);
		}
		return new ModelAndView("/check/lookSalesVolume", map);
	}

	// 供货商导出批量查看报表excel
	@RequestMapping(value = "/exportSalesVolumeForSupplier.do", method = { RequestMethod.POST })
	public void exportSalesVolumeForSupplier(HttpSession session, HttpServletResponse response, CheckVo checkVo) {

		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("excel", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

			HSSFWorkbook workbook = null;

			String path = session.getServletContext().getRealPath("") + System.getProperty("file.separator") + "jsp"
					+ System.getProperty("file.separator") + "templet" + System.getProperty("file.separator")
					+ "excelCheckSalesVolumeForSupplier.xls";
			InputStream fis = new FileInputStream(path);

			workbook = new HSSFWorkbook(fis);
			// 产生工作簿对象
			// 产生工作表对象
			HSSFSheet sheet = workbook.getSheet("Sheet1");

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			// 起始日期
			HSSFRow row1 = sheet.getRow(1);
			HSSFCell cell1FromRow1 = row1.getCell((int) 1);//
			cell1FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow1.setCellValue(checkVo.getOrdDateStart());
			cell1FromRow1.setCellStyle(setBorder);
			// 截止日期
			HSSFCell cell3FromRow1 = row1.getCell((int) 3);//
			cell3FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3FromRow1.setCellValue(checkVo.getOrdDateEnd());

			// 周期种类

			List<Code> codeList = dataDictionaryService.getCodeList("time_type");
			for (Code code : codeList) {
				if (Integer.valueOf(code.getCodeId()) == checkVo.getTimeType()) {
					checkVo.setTimeTypeString(code.getCodeName());
				}
			}

			HSSFCell cell4FromRow1 = row1.getCell((int) 5);//
			cell4FromRow1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell4FromRow1.setCellValue(checkVo.getTimeTypeString());

			HSSFCellStyle setBorderForcust = workbook.createCellStyle();
			setBorderForcust.setWrapText(true); // 自动换行
			setBorderForcust.setVerticalAlignment(setBorder.VERTICAL_CENTER);// 垂直居中
			setBorderForcust.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorderForcust.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorderForcust.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorderForcust.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
			setBorderForcust.setAlignment(HSSFCellStyle.VERTICAL_CENTER);

			// 材料名称
			HSSFRow row2 = sheet.getRow(2);
			HSSFCell cell1FromRow2 = row2.getCell((int) 1);//
			cell1FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell1FromRow2.setCellValue(materService.getMaterPo(checkVo.getMaterId()).getMaterName());
			cell1FromRow2.setCellStyle(setBorderForcust);
			// 材料单位
			HSSFCell cell2FromRow2 = row2.getCell((int) 3);//
			cell2FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell2FromRow2.setCellValue(materService.getMaterPo(checkVo.getMaterId()).getUnit());
			cell2FromRow2.setCellStyle(setBorderForcust);

			// 供货商
			UserPo userInfo = (UserPo) session.getAttribute("user");
			HSSFCell cell3FromRow2 = row2.getCell((int) 5);//
			cell3FromRow2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell3FromRow2.setCellValue(supplierService.getSupNameByUserId(userInfo.getUserId()));
			cell3FromRow2.setCellStyle(setBorderForcust);

			List<Object[]> listTotal = checkService.getSalesVolumeForExcel(checkVo);

			for (int i = 0; i < listTotal.size(); i++) {
				Object[] a = listTotal.get(i);

				HSSFRow row = sheet.createRow((int) i + 4);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(a[3].toString());
				cell1.setCellStyle(setBorder);

				// 补充格式
				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 2);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(a[0].toString());
				cell3.setCellStyle(setBorder);

			}

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}
}
